#include "editactions.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTextEdit>
#include <QClipboard>
#include <QApplication>

EditActions::EditActions(MainWindow *parent) : QObject(parent), mainWindow(parent) {}

void EditActions::setupEditActions() {
    // 初始化复制动作
    copyAction = new QAction(QIcon(":/icons/copy.png"), tr("复制"), this);
    copyAction->setShortcut(QKeySequence::Copy);
    connect(copyAction, &QAction::triggered, this, &EditActions::onActionCopyTriggered);

    // 初始化粘贴动作
    pasteAction = new QAction(QIcon(":/icons/paste.png"), tr("粘贴"), this);
    pasteAction->setShortcut(QKeySequence::Paste);
    connect(pasteAction, &QAction::triggered, this, &EditActions::onActionPasteTriggered);

    // 初始化剪切动作
    cutAction = new QAction(QIcon(":/icons/cut.png"), tr("剪切"), this);
    cutAction->setShortcut(QKeySequence::Cut);
    connect(cutAction, &QAction::triggered, this, &EditActions::onActionCutTriggered);

    // 初始化全选动作
    selectAllAction = new QAction(QIcon(":/icons/selectall.png"), tr("全选"), this);
    selectAllAction->setShortcut(QKeySequence::SelectAll);
    connect(selectAllAction, &QAction::triggered, this, &EditActions::onActionSelectAllTriggered);

    // 将动作添加到菜单
    mainWindow->ui->menuEdit->addAction(copyAction);  // 复制
    mainWindow->ui->menuEdit->addAction(pasteAction);   // 粘贴
    mainWindow->ui->menuEdit->addAction(cutAction);    // 剪切
    mainWindow->ui->menuEdit->addAction(selectAllAction);  // 全选

    // 将动作添加到工具栏（如果有）
    // mainWindow->ui->mainToolBar->addAction(copyAction);
    // mainWindow->ui->mainToolBar->addAction(pasteAction);
    // mainWindow->ui->mainToolBar->addAction(cutAction);
    // mainWindow->ui->mainToolBar->addAction(selectAllAction);
}

void EditActions::onActionCopyTriggered() {
    mainWindow->ui->textEdit->copy();
}

void EditActions::onActionPasteTriggered() {
    mainWindow->ui->textEdit->paste();
}

void EditActions::onActionCutTriggered() {
    mainWindow->ui->textEdit->cut();
}

void EditActions::onActionSelectAllTriggered() {
    mainWindow->ui->textEdit->selectAll();
}
